package com.song.osclient.qrcode;

import android.graphics.Bitmap;
import android.os.Handler;
import android.os.Message;

import com.google.zxing.BinaryBitmap;
import com.google.zxing.ReaderException;
import com.google.zxing.Result;
import com.google.zxing.common.HybridBinarizer;

/**
 * Created by Administrator on 2015/12/15.
 */
public class BitmapDecodeHandler extends DecodeHandler {

    public void start(final Bitmap bitmap, final Handler handler) {

        new Thread(new Runnable() {
            @Override
            public void run() {
                decode(bitmap, handler);
            }
        }).start();
    }

    private void decode(Bitmap mBitmap, Handler handler) {
        Result rawResult = null;
        try {
            rawResult = multiFormatReader.decodeWithState(new BinaryBitmap(new HybridBinarizer(new BitmapLuminanceSource(mBitmap))));
        } catch (ReaderException re) {
            // continue
        } finally {
            multiFormatReader.reset();
        }

        if (rawResult != null) {
            if (handler != null) {
                Message message = Message.obtain(handler, Constant.VALUE_DECODE_SUCCESS, rawResult.getText());
                message.sendToTarget();
            }
        } else {
            if (handler != null) {
                Message message = Message.obtain(handler, Constant.VALUE_DECODE_FAIL);
                message.sendToTarget();
            }
        }
    }
}
